/**
 * 
 */
package org.jft.datamodel.database.dao.implementation;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.jft.datamodel.config.ResourceLoader;
import org.jft.datamodel.database.HibernateUtil;
import org.jft.datamodel.database.dao.GenericDAO;
import org.jft.datamodel.database.mapping.Couple;
import org.jft.datamodel.database.mapping.Parents;
import org.jft.datamodel.database.mapping.Person;

/**
 * @author LAP
 *
 */
public class ParentsDAO extends GenericDAO<Parents> {

	@Override
	public Parents getByID(Long id) throws SQLException {
		Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession();
		Parents parent = (Parents) session.load(Parents.class, id);
		return null;
	}

	@Override
	public List<Parents> getAll() throws SQLException {
		Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession();
		List<Parents> parents = session.createCriteria(Parents.class).list();
		return null;
	}
	
	public void insertDescendant(Person descendant, Couple parent) throws HibernateException, IOException {
		Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.createSQLQuery(ResourceLoader.getInstance().getUsualSQLQuery("insert_descendant.sql"))
			.setLong("descendant_id", descendant.getID())
			.setLong("first_person_id", parent.getFirstPersonId())
			.setLong("second_person_id", parent.getSecodPersonId()).executeUpdate();
		session.getTransaction().commit();
	}
	
	public void deleteDescendant(Person descendant) throws HibernateException, IOException {
		Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.createSQLQuery(ResourceLoader.getInstance().getUsualSQLQuery("delete_descendant.sql"))
			.setLong("descendant_id", descendant.getID()).executeUpdate();
		session.getTransaction().commit();
	}
}
